package br.com.bluelogic.managebean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class ValidadorEmail implements Validator{
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." +
			                    "[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +
			                    "(\\.[A-Za-z]{2,})$";

public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException {
    String enteredEmail = (String)object;
    Pattern p = Pattern.compile(EMAIL_PATTERN);
    Matcher m = p.matcher(enteredEmail);
    boolean matchFound = m.matches();
    if (!matchFound) {
        FacesMessage message = new FacesMessage();
        message.setDetail("E-mail incorreto!");
        message.setSummary("E-mail incorreto!");
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(message);
    }
  }
}